<?php

/*
 * Plugin Name: TargetAI
 * Plugin URI: -----
 * Description: This plugin manages the AI tool that targets articles within topics
 * Version: 2.2.1
 * Author: Mark Tilly
 * Author URL: http://www.target-info.com
 * License: GPLv2 or later
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
define ('MCT_AI_REDIR','ailink');
define ('MCT_AI_LOG_ERROR','ERROR');
define ('MCT_AI_LOG_ACTIVITY','ACTIVITY');
define ('MCT_AI_LOG_PROCESS','PROCESS');

//Activation
register_activation_hook(__FILE__, 'mct_ai_activehook');
//Get options
$mct_ai_optarray = get_option('mct_ai_options');
//Set up menus
add_action('admin_menu', 'mct_ai_createmenu');
//Set up tag dropdown widget
add_action('widgets_init', 'mct_tag_select_register');
//Link manager, add rss column, change link entry form
if ($mct_ai_optarray['ai_on']) {
    add_filter('manage_link-manager_columns','mct_ai_linkcol');
    add_action('manage_link_custom_column','mct_ai_linkcolout',10,2);
    add_action('add_meta_boxes_link','mct_ai_linkeditmeta');
}
if ($mct_ai_optarray['ai_redir_comment']) {
    add_filter('comments_template','myc_cr_redir');
}
//Globals
$ai_topic_tbl = $wpdb->prefix.'topic';
$ai_postsread_tbl = $wpdb->base_prefix.'postsread';  //Only one master table so we can share all feeds across sites
if (empty($ai_sl_pages_tbl)) $ai_sl_pages_tbl = $wpdb->prefix.'sl_pages';
$ai_logs_tbl = $wpdb->prefix.'ai_logs';

//Get support functions
include('tgtai_posttypes.php');
include('tgtai_classify.php');
include('tgtai_fcns.php');

function mct_ai_activehook() {
    //Set up options
    global $mct_ai_optarray;
    
    $opt_update = array (
        'ai_on' => FALSE,
        'ai_log_days' => 7,
        'ai_excerpt' => 55,
        'ai_train_days' => 7
    );
    if (empty($mct_ai_optarray)){
        add_option('mct_ai_options',$opt_update);
    }
    //Create the data tables
    mct_ai_createdb();
    
    //register activation hooks
    mct_ai_register();
    
}

function mct_ai_linkcol($colarray) {
    $colarray['rss'] = __( 'Feed URL' );
    unset($colarray['rel']);
    unset($colarray['visible']);
    unset($colarray['rating']);
    return $colarray;
}
function mct_ai_linkcolout($colname, $linkid){
    global $wp_object_cache;
    
    if ($colname == 'rss'){
        $thecache = $wp_object_cache->cache;
        $thismark = $thecache['bookmark'];
        $thislink = $thismark[$linkid];
        $short_url = url_shorten( $thislink->link_rss );
        echo '<a href="'.$thislink->link_rss.'">'.$short_url.'</a>';
    }
}

function mct_ai_linkeditmeta($linkpg){
    //This function removes unneeded meta boxes on the link edit page and adds in a new meta box for just the rss link
    remove_meta_box('linktargetdiv', null, 'normal');
    remove_meta_box('linkxfndiv', null, 'normal');
    remove_meta_box('linkadvanceddiv', null, 'normal');
    add_meta_box('mctlinkrssfeed', __('Feed URL'), 'mct_ai_linkrssbox', null, 'normal', 'core');
}

function mct_ai_linkrssbox($link){
    //Adds the link rss meta box
    ?>
    <td><input name="link_rss" class="code" type="text" size="30" id="rss_uri" value="<?php echo  ( isset( $link->link_rss ) ? esc_attr($link->link_rss) : ''); ?>" size="50" style="width: 95%" /></td>
    <p><?php _e('Example: <code>http://feeds.feedburner.com/exampleblog</code> &#8212; don&#8217;t forget the <code>http://</code>'); ?></p>
<?php
}

function mct_ai_createmenu() {
   
    global $members_only_opt;
    
    add_menu_page('Topics', 'Topics','manage_links',__FILE__,'mct_ai_mainpage');
    add_submenu_page(__FILE__,'New Topic','New Topic','manage_links',__FILE__.'_topics','mct_ai_topicpage');
    add_submenu_page(__FILE__,'Topic Sources Manager','Topic Source','manage_links',__FILE__.'_topicsource','mct_ai_topicsource');
    add_submenu_page(__FILE__,'Options','Options','manage_links',__FILE__.'_options','mct_ai_optionpage');
    add_submenu_page(__FILE__,'Remove','Remove','manage_links',__FILE__.'_remove','mct_ai_removepage');
    if ($members_only_opt['members_only'] == TRUE) { 
        add_submenu_page(__FILE__,'Logins','Logins','manage_links',__FILE__.'_logins','mct_ai_feed_logins');
    }
    add_submenu_page(__FILE__,'Logs','Logs','manage_links',__FILE__.'_Logs','mct_ai_logspage');
    add_submenu_page(__FILE__,'Report','Report','manage_links',__FILE__.'_Report','mct_ai_logreport');
}

function mct_ai_mainpage() {
    global $wpdb, $ai_topic_tbl;
    
    // run ai process?
    if (isset($_POST['run_ai'])){
        include_once ('tgtai_process.php');
        mct_ai_process_site();
        echo '<h2>Target AI Process Completed</h2>';
        exit;
    }
    
    $editpage = $_SERVER['REQUEST_URI'] . '_topics&edit=';
    $sourcepage = $_SERVER['REQUEST_URI'] . '_topicsource&edit=';
    //Get Values from Db
    $sql = "SELECT `topic_name`, `topic_status`, `topic_type`, `topic_cat`, `topic_tag`, `topic_sources`
            FROM $ai_topic_tbl";
    $edit_vals = $wpdb->get_results($sql, ARRAY_A);
    
    ?>
    <div class='wrap'>
    <?php screen_icon('link-manager'); ?>
    <h2>MyCurator Topics</h2>  
    <form name="manual_ai" method="post" >
        <p><input name="run_ai" type="hidden" value="run_ai" /></p>
        <input name="run_ai_button" value="Run AI Process" type="submit" class="button-secondary">
        <br /><br />
    </form>
    <h4>Each of the MyCurator topics are listed below.  Click the Title to view or change any topic.  Click the Source field to add or update Sources.</h4>
        <table class="widefat" >
            <thead>
                <tr>
                <th>Topic</th>
                <th>Type</th>
                <th>Status</th>
                <th>Assigned Category</th>
                <th>Assigned Tag</th>
                <th>Sources</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($edit_vals as $row){
                echo('<tr>');
                echo('<td><a href="'.$editpage.trim($row['topic_name']).'" >'.$row['topic_name'].'</a></td>');
                echo('<td>'.$row['topic_type'].'</td>');
                echo('<td>'.$row['topic_status'].'</td>');
                echo('<td>'.get_cat_name($row['topic_cat']).'</td>');
                $tagterm = get_term($row['topic_tag'],'post_tag');
                echo('<td>'.$tagterm->name.'</td>');
                if (empty($row['topic_sources'])){
                    $source_fld = "Add Sources";
                } else {
                    $source_fld = "Edit Sources";
                }
                echo('<td><a href="'.$sourcepage.trim($row['topic_name']).'" >'.$source_fld.'</a></td>');
                echo('</tr>');
            } ?>
           </tbody>
        </table>
    </div>
<?php
}

function mct_ai_topicpage() {
    global $wpdb, $ai_topic_tbl;
    
    $pagetitle = 'New Topic';
    $update_type = 'false';  //set up for insert
    $msg = '';
    $edit_vals = array();
    $do_report = false;

    //Set up cat/tag dropdown
    $cats = array (
        'orderby' => 'name',
        'hide_empty' => FALSE,
        'name' => 'topic_cat'
    );
    $tags = array (
        'orderby' => 'name',
        'name' => 'topic_tag',
        'hide_empty' => FALSE,
        'show_option_none' => 'No Tags',
        'taxonomy' => 'post_tag'
    );
    if (isset($_POST['Submit']) ) {
        //sanitize values and update db
        //Create Slug if needed
        if (empty($_POST['topic_slug'])){
            $topicslug = sanitize_title($_POST['topic_name']);
        }
        //Clean up domains
        $valid_str = '';
        $varray = explode("\n",$_POST['topic_skip_domains']);
        foreach ($varray as $vstr){
            $vstr = trim(sanitize_text_field($vstr));
            if (strlen($vstr) != 0){
                $valid_str .= $vstr."\n";
            }
        }
        $valid_str = trim($valid_str);
        
        $val_array = array (
            'topic_slug' => $topicslug, 
            'topic_type' => trim(sanitize_text_field($_POST['topic_type'])),
            'topic_status' => trim(sanitize_text_field($_POST['topic_status'])), 
            'topic_search_1' => trim(stripslashes($_POST['topic_search_1'])),
            'topic_search_2' => trim(stripslashes($_POST['topic_search_2'])),
            'topic_exclude' => trim(stripslashes($_POST['topic_exclude'])),
            'topic_skip_domains' => $valid_str,
            'topic_cat' => strval(absint($_POST['topic_cat'])),
            'topic_tag' => strval(absint($_POST['topic_tag'])),
            'topic_tag_search2' => $_POST['topic_tag_search2'],
            'topic_min_length' => strval(absint($_POST['topic_min_length']))
        );
        if ($_GET['updated'] == 'true'){
            //Do an update
            $where = array('topic_name' => trim(sanitize_text_field($_POST['topic_name'])));
            $wpdb->update($ai_topic_tbl, $val_array, $where);
            $msg = "Topic Updated";
        } else {
            //Create News Feed and add as Source if checked
            if (!empty($_POST['topic_newsfeed'])) {
                //Set up array to pass to build fcn
                $v = array();
                $v['aname'] = trim(sanitize_text_field($_POST['topic_name'])).' News Feed'; //New Link Name
                $v['keywords'] = $val_array['topic_search_1']; //Keywords for the news search
                //Create the new link category using topic name 
                $theterm = wp_insert_term(trim(sanitize_text_field($_POST['topic_name'])).' Source','link_category');
                if (is_wp_error($theterm)){
                    $msg = $theterm->get_error_message();
                } else {
                    $v['link_category'] = $theterm['term_id'];
                }
                $linkval = bwc_do_news_create($v);
                if (is_string($linkval)) $msg = $linkval;
                if (empty($msg)) $val_array['topic_sources'] = $theterm['term_id']; //Load the new link category as source
            }
            //Create New Category if entered
            if (!empty($_POST['topic_createcat'])) {
                $theterm = wp_insert_term(trim(sanitize_text_field($_POST['topic_createcat'])),'category');
                if (is_wp_error($theterm)){
                    $msg = $theterm->get_error_message();
                } else {
                    $val_array['topic_cat'] = $theterm['term_id'];
                }
            }
            //Do an insert
            $val_array['topic_name'] = trim(sanitize_text_field($_POST['topic_name']));
            $wpdb->insert($ai_topic_tbl, $val_array);
            if (!empty($msg)) {
                $msg = 'Topic Added - Could Not Create News Feed: '.$msg;
            } else {
                $msg = "Topic Added";
            }
            //Add the new topic to the taxonomy database for the Target custom posts
            wp_insert_term($val_array['topic_name'],'topic');
            
        }
    }
    if (isset($_GET['edit'])){
        $tname = trim($_GET['edit']);
        $pagetitle = 'Edit Topic';
        $update_type = 'true'; //Means we have to do an update
        //Load values from db
        $sql = "SELECT `topic_name`, `topic_slug`, `topic_status`, `topic_type`, `topic_search_1`, `topic_search_2`, `topic_exclude`, `topic_skip_domains`, `topic_min_length`, `topic_cat`, `topic_tag`, `topic_tag_search2`
                FROM $ai_topic_tbl
                WHERE topic_name = '$tname'";
        $edit_vals = $wpdb->get_row($sql, ARRAY_A);
        //Set status dropdown
        $stat = $edit_vals['topic_status'];
        $typ = $edit_vals['topic_type'];
        $status_vals = array ($stat);
        switch ($stat) {
            case 'Inactive': 
                $status_vals[] = 'Training';
                break;
            case 'Training':
                $status_vals[] = 'Inactive';
                $status_vals[] = 'Active';
                break;
            case 'Active':
                $status_vals[] = 'Inactive';
                $status_vals[] = 'Training';
                break;
        }
        //Set up cat/tag dropdown
        $cats['selected'] = $edit_vals['topic_cat'];
        $tags['selected'] = $edit_vals['topic_tag'];
        //Set up Relevance report
        if ($typ == 'Relevance'  && $stat != 'Inactive'){
            $rel = new Relevance();
            $rpt = $rel->report($tname);
            if (!empty($rpt)) $do_report = true;
            unset($rel);
        }
        
    } else {
        $edit_vals['topic_status'] = 'Training';  //Set defaults
        $edit_vals['topic_type'] = 'Relevance';
        $status_vals[] = 'Inactive';
        $status_vals[] = 'Training';
    }
    
    ?>
    <div class='wrap'>
    <?php screen_icon('link-manager'); ?>
    <h2>MyCurator <?php echo $pagetitle; ?></h2>  
    <?php 
    if (!empty($msg)){ ?>
       <div id=”message” class="updated" ><p><strong><?php echo $msg ; ?></strong></p></div>
    <?php } ?>
       <p>Use spaces to separate keywords.  You can use phrases in Keywords by enclosing words in single or double quotes 
           (start and end quotes must be the same).  Use the root of a keyword and it will match all endings, for example manage 
           will match manages, manager and management.</p>
       <p>Press Save Options button at bottom to save your entries/changes</p>
       <form method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI'] . '&updated='.$update_type); ?>"> 
        <table class="form-table" >
            <?php if($update_type == 'false') { ?>
            <tr>
                <th scope="row">Topic Name</th>
                <td><input name="topic_name" type="input" id="topic_name" size="100" maxlength="200" value="<?php echo $edit_vals['topic_name']; ?>"  /></td>    
            </tr>
            <?php } else { ?>
            <tr>
                <th scope="row">Topic Name</th>
                <td><?php echo $edit_vals['topic_name']; ?></td>  
                <input name="topic_name" type="hidden" id="topic_name" value="<?php echo $edit_vals['topic_name']; ?>"  />
            </tr>
            <?php } ?>
            <tr>
                <th scope="row">Topic Search 1</th>
                <td><input name="topic_search_1" type="input" id="topic_search_1" size="100" value="<?php echo esc_attr($edit_vals['topic_search_1']); ?>"  />
                <span>&nbsp;<em>Each of these terms MUST be in the article</em></span></td>
            </tr>
            <tr>
                <th scope="row">Topic Search 2</th>
                <td><textarea name="topic_search_2" id="topic_search_2" rows="5" cols="100" ><?php echo esc_attr($edit_vals['topic_search_2']); ?></textarea>
                <span>&nbsp;<em>At Least 1 of these terms MUST be in the article</em></span></td>  
            </tr>
            <tr>
                <th scope="row">Topic Excluded</th>
                <td><input name="topic_exclude" type ="input" id="topic_exclude" size="100" value="<?php echo esc_attr($edit_vals['topic_exclude']); ?>"  />
                <span>&nbsp;<em>NONE of these terms may be in the article</em></span></td>    
            </tr><tr>
                <th scope="row">Minimum Article Length (in words)</th>
                <td><input name="topic_min_length" type="input" id="topic_min_length" size="5" maxlength="5" value="<?php echo $edit_vals['topic_min_length']; ?>"  /></td>    
            </tr>
            <tr>
                <th scope="row">Skip These Domains</th>
                <td><textarea id='topic_skip_domains' rows='5' cols='100' name='topic_skip_domains'><?php echo $edit_vals['topic_skip_domains'] ?></textarea></td>    
            </tr>
            <tr>
                <th scope="row">Choose Type</th>
                <td><select name="topic_type" >
                    <option value="Filter" <?php selected($edit_vals['topic_type'],"Filter"); ?>>Filter</option>
                    <option value="Relevance" <?php selected($edit_vals['topic_type'],"Relevance"); ?>>Relevance</option></select></td>    
            </tr>
            <tr>
                   <th scope="row">Topic Status</th>
                   <td><select name="topic_status" >
                <?php foreach ($status_vals as $stat) {
                        echo('<option value="'.$stat.'" '.selected($edit_vals['topic_status'],$stat).'>'.$stat.'</option>' );
                      }
                ?>
                </select></td>
            </tr>

            <tr>
                <th scope="row">Assign to Category</th>
                <td><?php wp_dropdown_categories($cats); ?><td>    
            </tr>
            <?php if ($update_type == 'false') { ?>
            <tr>
                <th scope="row">Or Create New Category</th>
                <td><input name="topic_createcat" type="input" id="topic_createcat" size="50" maxlength="200" /><span> (Will overide any Category chosen above)</span></td>    
            </tr> 
            <?php } ?>
            <tr>
                <th scope="row">Assign to Tag</th>
                <td><?php wp_dropdown_categories($tags); ?><td>    
            </tr> 
            <tr>
                <th scope="row">Use Search 2 Keywords as Tags?</th>
                <td><input name="topic_tag_search2" type="checkbox" id="topic_tag_search2" value="1" <?php checked('1', $edit_vals['topic_tag_search2']); ?> /><span> (Will override Tag chosen above)</span></td>    
            </tr> 
            <?php if ($update_type == 'false') { ?>
            <tr>
                <th scope="row">Use Custom Google News Feed?</th>
                <td><input name="topic_newsfeed" type="checkbox" id="topic_newsfeed" value="1" /><span> (Will use Search 1 Words to Create News Feed and apply as Source)</span></td>    
            </tr> 
            <?php }
            if ($do_report){  ?>
            <tr>
                <th scope="row">Relevance Good Items</th>
                <td><?php echo $rpt['good']; ?><td>  
            </tr>    
            <tr>
                <th scope="row">Relevance Bad Items</th>
                <td><?php echo $rpt['bad']; ?><td>  
            </tr>    
            <tr>
                <th scope="row">Relevance # of Words</th>
                <td><?php echo strval($rpt['dict']); ?><td>  
            </tr>  
            <tr>
                <th scope="row">Relevance DB Adjustment</th>
                <td><?php echo strval($rpt['shrinkdb']); ?><td>  
            </tr>    
            <tr>
                <th scope="row">Relevance Coefficient</th>
                <td><?php printf('%.4f',$rpt['coef']); ?><td>  
            </tr>    
            <?php } ?>

        </table>
            <!-- Topic Slug Hidden Fields -->
            <input name="topic_slug" type="hidden" id="topic_slug" value="<?php echo $edit_vals['topic_slug']; ?>" />
                    <div class="submit">
          <input name="Submit" type="submit" value="Save Options" class="button-primary" />
        </div>
       </form> 
    </div>
<?php
}

function mct_ai_optionpage() {
    //Always check if db created here in case it didn't happen - especially multi-user
    //since they have to come here at least once to turn on the system
    global $blog_id;
    mct_ai_createdb();
    $msg = '';
    if (isset($_POST['run_ai'])){
        include_once ('tgtai_process.php');
        mct_ai_process_all();
        exit;
    }
    
    if (isset($_POST['Submit']) ) {
        //load options into array and update db

        $opt_update = array (
            'ai_on' => ($_POST['ai_on'] == FALSE ? FALSE : TRUE),
            'ai_log_days' => absint($_POST['ai_log_days']),
            'ai_train_days' => absint($_POST['ai_train_days']),
            'ai_alt_theme' => absint($_POST['ai_alt_theme']),
            'ai_show_orig' => absint($_POST['ai_show_orig']),
            'ai_excerpt' => absint($_POST['ai_excerpt']),
            'ai_redir_comment' => absint($_POST['ai_redir_comment'])
        );
        update_option('mct_ai_options',$opt_update);
        $msg = 'Options have been updated';
    }
    //Get Options
    $cur_options = get_option('mct_ai_options');
    $cur_options['ai_excerpt'] = ($cur_options['ai_excerpt'] > 0) ? $cur_options['ai_excerpt'] : 55;
    ?>
    <div class='wrap'>
    <?php screen_icon('link-manager'); ?>
    <h2>MyCurator Setup</h2> 
    <?php if (!empty($msg)){ ?>
       <div id=”message” class="updated" ><p><strong><?php echo $msg ; ?></strong></p></div>
    <?php } ?>
    <?php
    
    //put up manual ai process button if user has authority
    $manual_ai = false;
    if ($cur_options['ai_on']){
        if (is_multisite()){
            if (current_user_can('manage_network') && $blog_id == 1){
                $manual_ai = true;
            }
        } 
        if ($manual_ai) { ?>
    <form name="manual_ai" method="post" >
        <p><input name="run_ai" type="hidden" value="run_ai" /></p>
        <input name="run_ai_button" value="Run AI Process" type="submit" class="button-secondary">
        <br /><br />
    </form>
<?php   }
    }
    ?>
    <p>Use this page to turn on the MyCurator system by checking below.  You can also set other options as described.</p>
        <form method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI'] . '&updated=true'); ?>" >
        <table class="form-table" >
            <tr>
                <th scope="row">Turn on MyCurator?</th>
                <td><input name="ai_on" type="checkbox" id="ai_on" value="1" <?php checked('1', $cur_options['ai_on']); ?>  /></td>    
            </tr>
            <tr>
                <th scope="row">Keep Log for How Many Days?</th>
                <td><input name="ai_log_days" type="text" id="ai_log_days" size ="5" value="<?php echo $cur_options['ai_log_days']; ?>"  /></td>    
            </tr>
            <tr>
                <th scope="row">Keep Training Posts for How Many Days?</th>
                <td><input name="ai_train_days" type="text" id="ai_train_days" size ="5" value="<?php echo $cur_options['ai_train_days']; ?>"  /></td>    
            </tr>            
            <tr>
                <th scope="row">Use an Alternate Theme?</th>
                <td><input name="ai_alt_theme" type="checkbox" id="ai_alt_theme" value="1" <?php checked('1', $cur_options['ai_alt_theme']); ?>  /></td>    
            </tr>
            <tr>
                <th scope="row">Show original article link, not readable page?</th>
                <td><input name="ai_show_orig" type="checkbox" id="ai_show_orig" value="1" <?php checked('1', $cur_options['ai_show_orig']); ?>  /></td>    
            </tr>
            <tr>
                <th scope="row">Excerpt length in words:</th>
                <td><input name="ai_excerpt" type="text" id="ai_excerpt" size ="5" value="<?php echo $cur_options['ai_excerpt']; ?>"  /></td>    
            </tr>    
            <tr>
                <th scope="row">Redirect Comments to Original Page?</th>
                <td><input name="ai_redir_comment" type="checkbox" id="ai_redir_comment" value="1" <?php checked('1', $cur_options['ai_redir_comment']); ?>  /></td>    
            </tr>
            
        </table>
        <div class="submit">
          <input name="Submit" type="submit" value="Save Options" class="button-primary" />
        </div>
        </form>
    </div>
<?php
}

function mct_ai_topicsource() {
    global $wpdb, $ai_topic_tbl;
    $tname = '';
    $msg = '';
    $sources = array ();
    if (isset($_GET['edit'])){
        $tname = trim(sanitize_text_field($_GET['edit']));
        $sql = "SELECT `topic_name`, `topic_sources`
            FROM $ai_topic_tbl
            WHERE topic_name = '$tname'";
        $edit_vals = $wpdb->get_row($sql, ARRAY_A);
        $sources = array_map('trim',explode(',',$edit_vals['topic_sources']));
    }
    if (isset($_POST['Submit']) ) {
        //load options into array and update db
        $tname = $_POST['topic_name'];
        $val_array = array('topic_sources' => implode(',',$_POST['sourceChk']));
        $where = array('topic_name' => $tname);
        $wpdb->update($ai_topic_tbl, $val_array, $where);
        $msg = "Sources have been updated";
    }
    if (isset($_POST['topic']) || isset($_POST['Submit'])){
        //Get Topic Values from Db
        if (isset($_POST['topic'])){
            $tname = trim(sanitize_text_field($_POST['topic_name']));
        }
        $sql = "SELECT `topic_name`, `topic_sources`
            FROM $ai_topic_tbl
            WHERE topic_name = '$tname'";
        $edit_vals = $wpdb->get_row($sql, ARRAY_A);
        $sources = array_map('trim',explode(',',$edit_vals['topic_sources']));
    }
    //Get all link categories
    $taxname = 'link_category';
    $terms = get_terms($taxname);
    //Get all topics for dropdown
    $sql = "SELECT `topic_name`
            FROM $ai_topic_tbl";
    $topic_vals = $wpdb->get_results($sql, ARRAY_A);
    ?>
    <div class='wrap'>
    <?php screen_icon('link-manager'); ?>
    <h2>MyCurator Topic Sources</h2>    
    <?php if (!empty($msg)){ ?>
       <div id=”message” class="updated" ><p><strong><?php echo $msg ; ?></strong></p></div>
    <?php } ?>
   <form name="select-topic" method='post'> 
	<input type="hidden" name="topic" value="select" />
        <select name="topic_name" >
          <?php foreach ($topic_vals as $topic){
              $topicnam = $topic['topic_name'];
               echo '<option value="'.$topicnam.'" '.selected($tname,$topicnam,false).'">'.$topicnam.'</option>';
          } ?>      
        </select>
	<input name="Select" value="Select Topic" type="submit" class="button-secondary">
        <em>Choose Topic and Click Select Topic to Edit Sources for that Topic</em>
   </form>
<?php if (!empty($tname)){ ?>
   <form name="sources" method='post'> 
       <h4>Select Each Source Below</h4>
       <table class="form-table" >
      <?php foreach ($terms as $term) {  ?>
           <tr>
                <td><input name="sourceChk[]" type="checkbox" value="<?php echo $term->term_id; ?> "
                 <?php if (!empty($sources) && in_array($term->term_id,$sources)) echo 'checked="checked"'; ?>/>
                 <?php echo '&nbsp;<strong>'.$term->name.'</strong>'; ?></td>
                <td><?php echo $term->description; ?></td>
           </tr>
      <?php } ?>
       </table>
       <p>
       <input type="hidden" name="topic_name" value="<?php echo $tname; ?>" />
       <input name="Submit" value="Submit" type="submit" class="button-primary"></p>
   </form>
    
<?php } ?>    
    </div>
<?php
}

function mct_ai_removepage() {
    global $wpdb, $ai_topic_tbl;
    //Need to remove topic, remove topics from taxonomy, any targets with this topic will eventually get cleard
    
    if (isset($_POST['Submit']) && isset($_POST['deleteChk']) ) {
        //load options into array and update db
        $val_array = $_POST['deleteChk'];
        if (!empty($val_array)) {
            foreach ($val_array as $delname){
                //Delete Topic
                $termobj = get_term_by('name',$delname, 'topic');
                wp_delete_term($termobj->term_id, 'topic');
                $wpdb->query($wpdb->prepare ("DELETE FROM $ai_topic_tbl WHERE topic_name = '$delname'" ));
                $a = '';
            }
        }
    }

    $sql = "SELECT `topic_name`, `topic_type`, `topic_cat`, `topic_tag`
            FROM $ai_topic_tbl
            WHERE `topic_status` = 'Inactive'";
    $edit_vals = $wpdb->get_results($sql, ARRAY_A);

    ?>
    <div class='wrap'>
    <?php screen_icon('link-manager'); ?>
    <h2>MyCurator Remove Topics</h2>  
    <h4>Each of the MyCurator topics that are currently inactive are listed below.  Click the checkbox next to any topic and it will be removed when you press Submit.</h4>
       <form name="select-topic" method='post'>
        <table class="widefat" >
            <thead>
                <tr>
                <th>Remove?</th>
                <th>Type</th>
                <th>Assigned Category</th>
                <th>Assigned Tag</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($edit_vals as $row){
                echo('<tr>');
                echo('<td><input name="deleteChk[]" type="checkbox" value="'.$row['topic_name'].'" />&nbsp;'.$row['topic_name'].'</td>');
                echo('<td>'.$row['topic_type'].'</td>');
                echo('<td>'.get_cat_name($row['topic_cat']).'</td>');
                $tagterm = get_term($row['topic_tag'],'post_tag');
                echo('<td>'.$tagterm->name.'</td>');
                echo('</tr>');
            } ?>
           </tbody>
        </table>
        <p>
        <input name="Submit" value="Submit" type="submit" class="button-primary"></p>
   </form>
    </div>
<?php
}

function mct_ai_logspage() {
    global $wpdb, $ai_logs_tbl, $ai_topic_tbl, $blog_id;
    
    $maxrow = 25;
    //Set current page from get
    $currentPage = 1;
    $topic = '';
    if (isset($_GET['paged'])){
        $currentPage = $_GET['paged'];
    }
    
    if (isset($_REQUEST['topic'])){
        if ($_REQUEST['topic'] == 'ALL'){
            $topic = '';
        } else {
            $topic = urldecode($_REQUEST['topic']);
        }
    }
    if (isset($_REQUEST['type'])){
        if ($_REQUEST['type'] == 'ALL'){
            $type = '';
        } else {
            $type = $_REQUEST['type'];
        }
    } else {
        $type = MCT_AI_LOG_ACTIVITY;  //default type
    }
    if (isset($_POST['Filter'])){
        $currentPage = 1;  //reset paging when a filter selected
    }
    //Get total rows available
    $sql = "SELECT COUNT(*) as myCount FROM " .$ai_logs_tbl;
    if (!empty($topic) && !empty($type)){
        $sql .= " WHERE `logs_topic` = '$topic' AND `logs_type` = '$type'";
    } else {
        if (!empty($topic)){
            $sql .= " WHERE `logs_topic` = '$topic'";
        }
        if (!empty($type)){
            $sql .= " WHERE `logs_type` = '$type'";
        }
    }
    $counts = $wpdb->get_row($sql,ARRAY_A);
    $myCount = $counts['myCount'];
    
    //Get all topics for dropdown
    $sql = "SELECT `topic_name`
            FROM $ai_topic_tbl";
    $topic_vals = $wpdb->get_results($sql, ARRAY_A);
    if ($blog_id == 1) $topic_vals[] = array('topic_name' => 'Blog');

    ?>
    <div class='wrap'>
    <?php screen_icon('link-manager'); ?>
    <h2>MyCurator Logs</h2>    
    <h4>Activity and Error Logs</h4>
    <?php
       print("<div class=\"tablenav\">"); 
       $qargs = array(
           'paged' => '%#%', 
           'topic' => urlencode($topic),
           'type' => $type);
       $page_links = paginate_links( array(
		'base' => add_query_arg($qargs ) ,
		'format' => '',
		'total' => ceil($myCount/$maxrow),
		'current' => $currentPage
	));
	
	if ( $page_links )
		echo "<div class='tablenav-pages'>$page_links</div>";
        //Select Topic
        echo '<div class = "alignleft" >';
        echo '<form id="Select" method="post" >';
        echo '<select name="topic">';
        echo '<option ';
        if (empty($topic)) echo 'SELECTED';
        echo ' value="ALL">View all Topics</option>';
        foreach ($topic_vals as $tops){
            $topicnam = $tops['topic_name'];
            echo '<option value="'.$topicnam.'" '.selected($topic,$topicnam,false).'>'.$topicnam.'</option>';
        }
        echo '</select>';
        echo '<select name="type">';
        echo '<option value="'.MCT_AI_LOG_PROCESS.'" '.selected($type,MCT_AI_LOG_PROCESS,false).'>'.MCT_AI_LOG_PROCESS.'</option>';
        echo '<option value="'.MCT_AI_LOG_ERROR.'" '.selected($type,MCT_AI_LOG_ERROR,false).'>'.MCT_AI_LOG_ERROR.'</option>';
        echo '<option value="'.MCT_AI_LOG_ACTIVITY.'" '.selected($type,MCT_AI_LOG_ACTIVITY,false).'>'.MCT_AI_LOG_ACTIVITY.'</option>';
        echo '</select>';
        echo '<input name="Filter" value="Select Filter" type="submit" class="button-secondary">';
        echo '</form></div>';
        
        //Get Values from Db
        $bottom = ($currentPage - 1) * $maxrow;
	$top = $currentPage * $maxrow;
        $sql = "SELECT `logs_date`, `logs_topic`, `logs_type`, `logs_msg`, `logs_url`
            FROM $ai_logs_tbl ";
        if (!empty($topic) && !empty($type)){
            $sql .= " WHERE `logs_topic` = '$topic' AND `logs_type` = '$type'";
        } else {
            if (!empty($topic)){
                $sql .= " WHERE `logs_topic` = '$topic'";
            }
            if (!empty($type)){
                $sql .= " WHERE `logs_type` = '$type'";
            }
        }
        $sql .= " ORDER BY `logs_date` DESC LIMIT " . $bottom . "," . $maxrow;
        $edit_vals = array();
        $edit_vals = $wpdb->get_results($sql, ARRAY_A);
        ?>

        <table class="widefat" >
            <thead>
                <tr>
                <th>Date</th>
                <th>Topic</th>
                <th>Type</th>
                <th>Message</th>
                <th>URL</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($edit_vals as $row){
                echo('<tr');
                if ($alter) {
		 	$alter = false;
		 	print(" class='alternate' ");
		} else {
			$alter = true;
		}
                echo ('>');
                echo('<td>'.$row['logs_date'].'</td>');
                echo('<td>'.$row['logs_topic'].'</td>');
                echo('<td>'.$row['logs_type'].'</td>');
                echo('<td>'.$row['logs_msg'].'</td>');
                if (!empty($row['logs_url'])){
                    echo('<td><a href="'.$row['logs_url'].'" >'.$row['logs_url'].'</a>');
                }
                echo('</tr>');
            } ?>
           </tbody>
        </table>
    </div>
<?php
}

function mct_ai_logreport(){
    //Print out pie chart based on activity log
    //Use google pie chart api as an image
    global $wpdb, $mct_ai_optarray, $ai_postsread_tbl, $ai_sl_pages_tbl, $ai_logs_tbl;
    
    $sql = "SELECT `logs_msg`
            FROM $ai_logs_tbl WHERE `logs_type` = '".MCT_AI_LOG_ACTIVITY."' ";
    $logs = $wpdb->get_results($sql, ARRAY_A);
    $cnts = array();
    $tot = 0;
    foreach ($logs as $log){
        $tot += 1;
        if (stripos($log['logs_msg'],'Live') !== false) $cnts['Live'] += 1;
        if (stripos($log['logs_msg'],'short') !== false) $cnts['Too Short'] += 1;
        if (stripos($log['logs_msg'],'excluded') !== false) $cnts['Excluded'] += 1;
        if (stripos($log['logs_msg'],'search 1') !== false) $cnts['No Search 1'] += 1;
        if (stripos($log['logs_msg'],'search 2') !== false) $cnts['No Search 2'] += 1;
        if (stripos($log['logs_msg'],'Training') !== false) $cnts['Training'] += 1;
        if (stripos($log['logs_msg'],'good') !== false) $cnts['good'] += 1;
        if (stripos($log['logs_msg'],'bad') !== false) $cnts['bad'] += 1;
        if (stripos($log['logs_msg'],'not sure') !== false) $cnts['not sure'] += 1;
    }
    $name = "";
    $pcts = "";
    foreach ($cnts as $key => $val){
        $pctstr = strval(intval(($val/$tot)*100));
        $name .= $key." - ".$pctstr."|";
        $pcts .= $pctstr.",";
    }
    $name = substr($name,0,strlen($name)-1);
    $pcts = "t:".substr($pcts,0,strlen($pcts)-1);
    $outstr = '<img src="http://chart.apis.google.com/chart?cht=p3&chd='.$pcts.'&chs=700x300&chl='.$name.'" style="max-width: 100%;margin: 5px" />';
    
    echo     "<div class='wrap'>";
    echo "<h2>MyCurator Article Classification Report (numbers are %)</h2>";
    echo "<h3>Out of ".strval($tot)." Articles read over the last ".strval($mct_ai_optarray['ai_log_days'])." days";
    echo "<br /><br /><br />";
    echo $outstr;
    
}

function mct_ai_createdb(){
    global $wpdb, $ai_topic_tbl, $ai_postsread_tbl, $ai_sl_pages_tbl, $ai_logs_tbl, $mct_ai_fdvals_tbl;
    
    $sql = "CREATE TABLE `$ai_topic_tbl` (
            `topic_id` int(11) NOT NULL AUTO_INCREMENT,
            `topic_name` varchar(200) NOT NULL,
            `topic_slug` varchar(200) NOT NULL, 
            `topic_status` varchar(20) NOT NULL,
            `topic_type` varchar(20) NOT NULL,
            `topic_search_1` text,
            `topic_search_2` text,
            `topic_exclude` text,
            `topic_sources` longtext,
            `topic_aidbfc` longtext,
            `topic_aidbcat` longtext,
            `topic_skip_domains` longtext,
            `topic_min_length` int(11),
            `topic_cat` int(11),
            `topic_tag` int(11),
            `topic_tag_search2` char(1),
            `topic_last_run` DATETIME,
            PRIMARY KEY (`topic_id`)
    ) DEFAULT CHARSET=utf8 ;";
    
    require_once(ABSPATH.'wp-admin/includes/upgrade.php');
    dbDelta($sql);
    
    $sql = "CREATE TABLE `$ai_postsread_tbl` (
            `pr_id` int(11) NOT NULL AUTO_INCREMENT,
            `pr_url` varchar(1000) NOT NULL,
            `pr_date` DATETIME NOT NULL,
            `pr_topics` varchar(50),
            `pr_page_content` longtext,
            PRIMARY KEY (`pr_id`),
            INDEX `pr_url` (`pr_url`)
    ) DEFAULT CHARSET=utf8 ;";
    dbDelta($sql);
    
    $sql = "CREATE TABLE `$ai_logs_tbl` (
            `logs_id` int(11) NOT NULL AUTO_INCREMENT,
            `logs_date` DATETIME NOT NULL,
            `logs_type` varchar(20) NOT NULL,
            `logs_topic` varchar(200) NOT NULL,
            `logs_url` varchar(1000),
            `logs_msg` varchar(200) NOT NULL,
            `logs_aiclass` varchar(20),
            `logs_aigood` FLOAT(5,4),
            `logs_aibad` FLOAT(5,4),
            PRIMARY KEY (`logs_id`)
    ) DEFAULT CHARSET=utf8 ;";
    dbDelta($sql);
    
    $sql = "CREATE TABLE `$mct_ai_fdvals_tbl` (
            `fdv_id` int(11) NOT NULL AUTO_INCREMENT,
            `fdv_name` varchar(200) NOT NULL,
            `fdv_domain` varchar(200) NOT NULL,
            `fdv_ustr` varchar(200) NOT NULL,
            `fdv_pstr` varchar(200)NOT NULL,
            `fdv_faildom` varchar(200) NOT NULL,
            `fdv_regurl` varchar(200) NOT NULL,
            `fdv_func` varchar(50),
            `fdv_cookie` varchar(50) NOT NULL,
            PRIMARY KEY (`fdv_id`)
    ) DEFAULT CHARSET=utf8 ;";
    dbDelta($sql);
}

function mct_ai_getsavedpage($sl_id){
    //this function returns the page content for a saved page given an id
    global $ai_sl_pages_tbl, $wpdb;
    
    $sql = "SELECT `sl_page_content`
            FROM $ai_sl_pages_tbl
            WHERE sl_page_id = '$sl_id'";
    $vals = $wpdb->get_row($sql, ARRAY_A);
    return $vals['sl_page_content'];
}

function mct_ai_getsavedpageid($postid){
    //this function returns an array of int saved page id's for the passed in post
    $sl_id = array();
    $newlinks = get_post_meta($postid,'mct_sl_newurl',true);
    if (!empty($newlinks)){
        foreach ($newlinks as $nlink){
            $pos = preg_match('{/'.MCT_AI_REDIR.'/(.*)$}',$nlink,$matches);  //stripos($newlinks[$i],'ailink/');
            if ($pos){
                $sl_id[] = intval(trim($matches[1]));
            }
        }
    }
    return $sl_id;
}

function mct_tag_select_register() {
    register_widget('mct_tag_selectwidget');
}

function myc_cr_redir($file){
    global $post;
    //This function redirects comments to the original web page of the post    
    
    $cnt = preg_match_all('{<a\s(.*)/ailink/[0-9]+"\s*>}',$post->post_content,$matches);
    if ($cnt == 1) {
        $file = plugin_dir_path( __FILE__).'nocomment.php';
    }
    return $file;
}


class mct_tag_selectwidget extends WP_Widget {
    
    function mct_tag_selectwidget() {
        global $mct_ai_optarray;
        
        $widget_ops = array (
            'classname' => 'mct_tag_selectwidget_class',
            'description' => 'Select Tags'
        );
        $this->WP_Widget('mct_tag_selectwidget', "Tag Dropdown", $widget_ops);
    }
    
    function form($instance) {
        $defaults = array('title' => 'Select Tags', 'name' => 'Tags');
        $instance = wp_parse_args( (array) $instance, $defaults);
        $title = $instance['title'];
        $name = $instance['name'];
        ?><p>Title: <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text"
                         value="<?php echo esc_attr($title); ?>" /></p>
        <p>Title: <input class="widefat" name="<?php echo $this->get_field_name('name'); ?>" type="text"
                         value="<?php echo esc_attr($name); ?>" /></p>
        
        <?php
    }
    
    function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['name'] = strip_tags($new_instance['name']);
        return $instance;
    }
    
    function widget($args,$instance){
        global $mct_ai_optarray;
        extract ($args);
        //Get uri, strip out previous gets
        $tagargs = array (
            'orderby' => 'name'
        );
        echo $before_widget;
        $title = apply_filters('widget_title',$instance['title']);
        echo $before_title.$title.$after_title;
        //wp_dropdown_categories($tags);
        echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
        echo "<option>".$instance['name']."</option>\n";
        $alltags = get_tags($tagargs);
        foreach ($alltags as $tag)
        {
            echo "<option value=\"";
            echo get_tag_link($tag->term_id);
            echo "\">".$tag->name."</option>\n";
        }
        echo "</select>";        
        echo $after_widget;
    }
}


$stopwords = array('a', 'about', 'above', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'all', 'almost', 'alone',
 'along', 'already', 'also','although','always','am','among', 'amongst', 'amoungst', 'amount',  'an', 'and', 'another', 'any',
'anyhow','anyone','anything','anyway', 'anywhere', 'are', 'around', 'as',  'at', 'back','be','became', 'because','become',
'becomes', 'becoming', 'been', 'before', 'beforehand', 'behind', 'being', 'below', 'beside', 'besides', 'between', 'beyond', 
'bill', 'both', 'bottom','but', 'by', 'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de', 'describe',
 'detail', 'do', 'done', 'down', 'due', 'during', 'each', 'eg', 'eight', 'either', 'eleven','else', 'elsewhere', 'empty', 'enough'
, 'etc', 'even', 'ever', 'every', 'everyone', 'everything', 'everywhere', 'except', 'few', 'fifteen', 'fify', 'fill', 'find', 
'fire', 'first', 'five', 'for', 'former', 'formerly', 'forty', 'found', 'four', 'from', 'front', 'full', 'further', 'get', 
'give', 'go', 'had', 'has', 'hasnt', 'have', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers',
 'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'ie', 'if', 'in', 'inc', 'indeed', 'interest', 'into', 'is', 
'it', 'its', 'itself', 'keep', 'last', 'latter', 'latterly', 'least', 'less', 'ltd', 'made', 'many', 'may', 'me', 'meanwhile', 
'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must', 'my', 'myself', 'name', 'namely', 'neither', 
 'never', 'nevertheless', 'next', 'nine', 'no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off', 
 'often', 'on', 'once', 'one', 'only', 'onto', 'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'own',
 'part', 'per', 'perhaps', 'please', 'put', 'rather', 're', 'same', 'see', 'seem', 'seemed', 'seeming', 'seems', 'serious', 'several',
 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so', 'some', 'somehow', 'someone', 'something', 'sometime', 
 'sometimes', 'somewhere', 'still', 'such', 'system', 'take', 'ten', 'than', 'that', 'the', 'their', 'them', 'themselves', 'then', 
 'thence', 'there', 'thereafter', 'thereby', 'therefore', 'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this',
 'those', 'though', 'three', 'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too', 'top', 'toward', 'towards', 'twelve', 
  'twenty', 'two', 'un', 'under', 'until', 'up', 'upon', 'us', 'very', 'via', 'was', 'we', 'well',
 'were', 'what', 'whatever', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon', 
'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever', 'whole', 'whom', 'whose', 'why', 'will', 'with', 'within',
 'without', 'would', 'yet', 'you', 'your', 'yours', 'yourself', 'yourselves', 'the');

$threeletter = array('new');
?>